package com.gduf.gulimall.product.service.impl;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gduf.common.constant.ProductConstant;
import com.gduf.gulimall.product.dao.AttrAttrgroupRelationDao;
import com.gduf.gulimall.product.dao.AttrGroupDao;
import com.gduf.gulimall.product.dao.CategoryDao;
import com.gduf.gulimall.product.entity.AttrAttrgroupRelationEntity;
import com.gduf.gulimall.product.entity.AttrGroupEntity;
import com.gduf.gulimall.product.entity.CategoryEntity;
import com.gduf.gulimall.product.entity.vo.AttrReponseVo;
import com.gduf.gulimall.product.service.CategoryService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gduf.common.utils.PageUtils;
import com.gduf.common.utils.Query;

import com.gduf.gulimall.product.dao.AttrDao;
import com.gduf.gulimall.product.entity.AttrEntity;
import com.gduf.gulimall.product.service.AttrService;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;


@Service("attrService")
public class AttrServiceImpl extends ServiceImpl<AttrDao, AttrEntity> implements AttrService {

    @Resource
    private AttrGroupDao attrGroupDao;

    @Resource
    private CategoryDao categoryDao;

    @Resource
    private AttrAttrgroupRelationDao relationDao;

    @Resource
    private CategoryService categoryService;

    @Resource
    private AttrDao attrDao;




    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrEntity> page = this.page(
                new Query<AttrEntity>().getPage(params),
                new QueryWrapper<AttrEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryBaseAttrPage(Map<String, Object> params, Long catelogId) {

        QueryWrapper<AttrEntity> queryWrapper = new QueryWrapper<>();
        if(catelogId != 0) queryWrapper.eq("catelog_id",catelogId);

        String key = (String)params.get("key");
        if(!StringUtils.isEmpty(key)){
            //两者是一个整体条件
            queryWrapper.and((wrapper)->{
                wrapper.eq("attr_id",key).or().like("attr_name",key);
            });
        }

        IPage<AttrEntity> page = this.page(
                new Query<AttrEntity>().getPage(params),
                queryWrapper
        );



        PageUtils pageUtils = new PageUtils(page);
        List<AttrEntity> records = page.getRecords();

        List<AttrReponseVo> reponseVos = records.stream().map((attrEntity) -> {
            AttrReponseVo attrReponseVo = new AttrReponseVo();
            System.out.println("attrEntity为"+attrEntity);
            BeanUtils.copyProperties(attrEntity, attrReponseVo);
            System.out.println("attrReponseVo"+attrReponseVo);
            //设置分组名字和分类名字
            AttrAttrgroupRelationEntity attrId = relationDao.selectOne(new QueryWrapper<AttrAttrgroupRelationEntity>().eq("attr_id", attrEntity.getAttrId()));
            if (attrId != null) {
                Long attrGroupId = attrId.getAttrGroupId();
                attrReponseVo.setGroupName(attrGroupDao.selectById(attrGroupId).getAttrGroupName());
            }
            CategoryEntity categoryEntity = categoryDao.selectById(attrEntity.getCatelogId());
            if (categoryEntity != null) {
                attrReponseVo.setCatelogName(categoryEntity.getName());
            }
            return attrReponseVo;
        }).collect(Collectors.toList());

        pageUtils.setList(reponseVos);
        return pageUtils;
    }

    @Override
    public AttrReponseVo getAttrInfo(Long attrId) {
        AttrReponseVo reponseVo = new AttrReponseVo();
        AttrEntity attrEntity = this.getById(attrId);
        BeanUtils.copyProperties(attrEntity,reponseVo);

        //分组信息
        AttrAttrgroupRelationEntity attrAttrgroupRelationEntity = relationDao.selectOne(new QueryWrapper<AttrAttrgroupRelationEntity>().eq("attr_id", attrEntity.getAttrId()));
        if(attrAttrgroupRelationEntity != null){
            reponseVo.setAttrGroupId(attrAttrgroupRelationEntity.getAttrGroupId());
            AttrGroupEntity attrGroupEntity = attrGroupDao.selectById(attrAttrgroupRelationEntity.getAttrGroupId());
            reponseVo.setGroupName(attrGroupEntity.getAttrGroupName());
        }

        //分类信息
        Long catelogId = attrEntity.getCatelogId();
        Long[] catelogPath = categoryService.findCatelogPath(catelogId);
        reponseVo.setCatelogPath(catelogPath);
        CategoryEntity categoryEntity = categoryDao.selectById(catelogId);
        reponseVo.setCatelogName(categoryEntity.getName());


        return reponseVo;
    }

    @Override
    public List<AttrEntity> getRelationAttr(long attrgroupId) {
        //获取所有的ids
        List<AttrAttrgroupRelationEntity> entities = relationDao.selectList(new QueryWrapper<AttrAttrgroupRelationEntity>().eq("attr_group_id", attrgroupId));
        List<Long> ids = entities.stream().map((item) -> {
            return item.getAttrId();
        }).collect(Collectors.toList());

        //利用ids获取Attr的集合
        List<AttrEntity> attrEntities = null;
        if(!CollectionUtils.isEmpty(ids)){
            attrEntities = attrDao.selectBatchIds(ids);
        }
        return attrEntities;
    }

    @Override
    public List<Long> selectSearchAttrs(List<Long> attrIds) {

        return baseMapper.selectSearchAttrIds(attrIds);

    }


}